package core;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import towers.Bunker;
import towers.Cannon;
import towers.MissileLauncher;
import towers.NuclearBase;
import towers.Plasmagun;
import towers.Tower;

/**
 * @author Robbie Groenewoudt
 *
 */
public class ViewMenu extends JPanel implements Observer {
	
	private Controller controller;
	private Model model;
	
	// Achtergrond kleur is bijna overal gelijk, hele menu wordt namelijk deze kleur
	private Color backgroundColor = new Color(60, 79, 104);
	
	private JLabel towerImageLabel = new JLabel();
	private JLabel towerNameLabel = new JLabel("No tower");
	private JLabel towerPriceLabel = new JLabel("");
	private JLabel towerPowerLabel = new JLabel("");
	private JLabel towerRangeLabel = new JLabel("");
	private JLabel towerFirerateLabel = new JLabel("");
	private JLabel towerHealthLabel = new JLabel("/");
	private JLabel killsLabel = new JLabel("0");
	private JLabel levelLabel = new JLabel("1");
	private JLabel scoreLabel = new JLabel("0");
	private JLabel cashLabel = new JLabel("0");
	private JLabel waveLabel = new JLabel("10 sec");
	private JButton towerRepair;
	private MenuButton towerSell;
	private JLabel healthLabel = new JLabel("");
	
	// Tower bouw knoppen
	private ArrayList<JButton> towers = new ArrayList<JButton>(5);
	
	public ViewMenu(Model m, Controller c) {
		this.controller = c;
		this.model = m;
		
		this.setLayout( new BorderLayout() );
		this.setPreferredSize( new Dimension(210, 485));
		this.setBackground( backgroundColor );
		
		// We hebben een CENTER en SOUTH
		// In SOUTH komen de brede ronde rode buttons
		// In CENTER de rest
		JPanel center = new JPanel( new GridLayout(3,1) );
		center.setBackground( backgroundColor );
		
		this.add(center, BorderLayout.CENTER);
		this.add(createMenuButtonsPanel(), BorderLayout.SOUTH);
		
		// CENTER vullen
		center.add( createTowerBuildButtonsPanel() );
		center.add( createMenu("Tower stats", new TowerStatsPanel()) );
		center.add( createStatsPanel() );
	}
	
	private JPanel createMenuButtonsPanel() {
		JPanel x = new JPanel(new GridLayout(4,1));
		x.setBackground( backgroundColor );
		x.add( new MenuButton("Pause", new ImageIcon("images/buttons/pause.gif"), this.controller) );
		x.add( new MenuButton("Restart", new ImageIcon("images/buttons/restart.gif"), this.controller) );
		x.add( new MenuButton("Abandon", new ImageIcon("images/buttons/abandon.gif"), this.controller) );
		x.add( new MenuButton("Next Wave", new ImageIcon("images/buttons/nextwave.gif"), this.controller) );
		
		return x;
	}
	
	private JPanel createTowerBuildButtonsPanel() {
		JPanel x = new JPanel();
		x.setBackground( backgroundColor );
		x.setPreferredSize(new Dimension(210,100));
		
		// Voeg tower-JButtons toe aan ArrayList
		towers.add( new TowerBuildButton("Bunker", new ImageIcon("images/bunker.gif"), this.controller ) );
		towers.add( new TowerBuildButton("Cannon", new ImageIcon("images/cannon-button.gif"), this.controller ) );
		towers.add( new TowerBuildButton("Plasmagun", new ImageIcon("images/plasmagun-button.gif"), this.controller ) );
		towers.add( new TowerBuildButton("Missile Launcher", new ImageIcon("images/missilelauncher.gif"), this.controller ) );
		towers.add( new TowerBuildButton("Nuclear Base", new ImageIcon("images/nuclearbase-button.gif"), this.controller ) );
		
		// Voeg dezelfde buttons ook toe aan de JPanel
		for(JButton j : towers) {
			x.add(j);
		}
		
		return x;
	}
	
	private JPanel createStatsPanel() {
		// Double buffering graag aangezien deze panel ook vaak kan veranderen
		JPanel content = new JPanel( new GridLayout(5,1), true );
		content.setBorder( new EmptyBorder(10, 25, 10, 25) );
		
		JLabel panel1 = new JLabel("Kills:");
		panel1.setForeground( Color.white );
		content.add( panel1 );
		killsLabel.setForeground( Color.white );
		content.add( killsLabel );
		
		JLabel panel2 = new JLabel("Level:");
		panel2.setForeground( Color.white );
		content.add( panel2 );
		levelLabel.setForeground( Color.white );
		content.add( levelLabel );
		
		JLabel panel3 = new JLabel("Score:");
		panel3.setForeground( Color.white );
		content.add( panel3 );
		scoreLabel.setForeground( Color.white );
		content.add( scoreLabel );
		
		JLabel panel4 = new JLabel("Cash:");
		panel4.setForeground( Color.white );
		content.add( panel4 );
		cashLabel.setForeground( Color.white );
		content.add( cashLabel );
		
		JLabel panel5 = new JLabel("Wave:");
		panel5.setForeground( Color.white );
		content.add( panel5 );
		waveLabel.setForeground( Color.white );
		content.add( waveLabel );
		
		return createMenu("Statistics", content);
	}
	// Helper-functie om makkelijk een "menuutje" aan te maken, samen met een zwarte titelbalk
	private JPanel createMenu( String titleText, JPanel content ) {
		JPanel x = new JPanel( new BorderLayout() );
		x.setBackground( backgroundColor );
		
		// Titel
		JPanel title = new JPanel();
		title.setBackground( Color.black );
		JLabel titleLabel = new JLabel( titleText );
		titleLabel.setForeground( Color.white );
		titleLabel.setFont( new Font("Verdana",Font.BOLD, 12) );
		title.add( titleLabel );
		x.add( title, BorderLayout.NORTH );
		
		// Content
		content.setBackground( backgroundColor );
		x.add( content, BorderLayout.CENTER );
		
		return x;
	}
	
	public void update(Observable arg0, Object arg1) {
		if( this.controller.getMouse().getSelectedItem() != null){

			Tower selected = this.controller.getMouse().getSelectedItem();
			
			if(selected.getHealth() == 0){
				this.controller.getMouse().setSelectedItem(null);
			}
			
			if(selected instanceof Bunker){
				towerImageLabel.setIcon( new ImageIcon("images/bunker.gif") );
				towerNameLabel.setText("Bunker");
			}
			else if(selected instanceof Cannon){
				towerImageLabel.setIcon(new ImageIcon("images/cannon-button.gif"));
				towerNameLabel.setText("Cannon");
			}
			else if(selected instanceof Plasmagun){
				towerImageLabel.setIcon(new ImageIcon("images/plasmagun-button.gif"));
				towerNameLabel.setText("Plasmagun");
			}
			else if(selected instanceof MissileLauncher){
				towerImageLabel.setIcon(new ImageIcon("images/missilelauncher.gif"));
				towerNameLabel.setText("Missile Launcher");
			}
			else if(selected instanceof NuclearBase){
				towerImageLabel.setIcon(new ImageIcon("images/nuclearbase-button.gif"));
				towerNameLabel.setText("Nuclearbase");
			}
			
			healthLabel.setText("health");
			towerPriceLabel.setText("Price: " + selected.getCost());
			towerPowerLabel.setText("Power: " + selected.getCost()); // TODO
			towerRangeLabel.setText("Range: " + selected.getRange());
			towerFirerateLabel.setText("Fire rate: " + selected.getFirerate());
			towerHealthLabel.setText(selected.getHealth() + "/" + selected.getMaxHealth());
			towerSell.setVisible(true);
			towerRepair.setVisible(true);
			
			// Repairknop disablen als het niet gerepareerd kan worden
			if ( selected.getHealth() == selected.getMaxHealth() )
				towerRepair.setEnabled(false);
			else
				towerRepair.setEnabled(true);
			
			// Sellknop disablen als het niet geplaatst is
			if ( selected.getPosition() == null )
				towerSell.setEnabled(false);
			else
				towerSell.setEnabled(true);
		} else {
			towerImageLabel.setIcon(null);
			towerNameLabel.setText("no tower selected");
			healthLabel.setText("");
			towerPriceLabel.setText("");
			towerPowerLabel.setText("");
			towerRangeLabel.setText("");
			towerFirerateLabel.setText("");
			towerHealthLabel.setText("");
			towerRepair.setVisible(false);
			towerSell.setVisible(false);
		}
		
		killsLabel.setText( "" + this.model.getStats().getKills() );
		levelLabel.setText( "" + this.model.getStats().getLevel() );
		cashLabel.setText( "" + this.model.getStats().getMoney() );
		waveLabel.setText( (this.model.getStats().getNextWave() + 1) + " sec" );
		
		// Update toren-bouw-knoppen, disable als je niet genoeg geld hebt
		for( JButton x : this.towers ) {
			int costTower = 0;
			
			if ( x.getActionCommand().equals( "Cannon" )) {
				costTower = Cannon.cost;
			}
			else if ( x.getActionCommand().equals( "Bunker" )) {
				costTower = Bunker.cost;
			}
			else if ( x.getActionCommand().equals( "Missile Launcher" )) {
				costTower = MissileLauncher.cost;
			}
			else if ( x.getActionCommand().equals( "Nuclear Base" )) {
				costTower = NuclearBase.cost;
			}
			else if ( x.getActionCommand().equals( "Plasmagun" )) {
				costTower = Plasmagun.cost;
			}
			
			if ( model.getStats().getMoney() >= costTower ) {
				x.setEnabled( true );
				x.setBackground( new Color(15, 4, 254) ); // Achtergrond weer blauw
			}
			else {
				x.setEnabled( false );
				x.setBackground( backgroundColor );
			}
		}
	}
	
	// Brede ronde rode button
	class MenuButton extends JButton {
		private MenuButton( String action, ImageIcon img, Controller c ) {
			this.addActionListener( c );
			this.setIcon( img );
			this.setActionCommand( action );
			this.setSize( new Dimension(200, 18) );
			this.setBackground( new Color(255, 14, 16) );
			this.setForeground( Color.WHITE );
			this.setContentAreaFilled(false);
			this.setBorder(BorderFactory.createEmptyBorder());
			this.setPreferredSize( new Dimension(200, 25) );
			this.setFocusable( false );
		}
	}
	
	class TowerBuildButton extends JButton {
		private TowerBuildButton( String action, ImageIcon img, Controller c ) {
			this.addActionListener(c);
			this.setBackground( new Color(15, 4, 254) );
			this.setIcon( img );
			this.setPreferredSize( new Dimension(50, 50) );
			this.setActionCommand( action );
			this.setFocusable( false );
			this.setToolTipText("Build " + action);
		}
		
		public void paintComponent( Graphics g ) {
			super.paintComponent(g);
			
			// Draw prijs rechtsonderin
			int costTower = 0;
			
			if ( getActionCommand().equals( "Cannon" )) {
				costTower = Cannon.cost;
			}
			else if ( getActionCommand().equals( "Bunker" )) {
				costTower = Bunker.cost;
			}
			else if ( getActionCommand().equals( "Missile Launcher" )) {
				costTower = MissileLauncher.cost;
			}
			else if ( getActionCommand().equals( "Nuclear Base" )) {
				costTower = NuclearBase.cost;
			}
			else if ( getActionCommand().equals( "Plasmagun" )) {
				costTower = Plasmagun.cost;
			}
			
			if( costTower > 0 ) {
				// Shaduw
				g.setColor( Color.black );
				g.setFont( new Font("Tahoma", Font.PLAIN, 9) );
				g.drawString( "" + costTower , 28, 48);
				
				
				g.setColor( Color.white );
				g.setFont( new Font("Tahoma", Font.PLAIN, 9) );
				g.drawString( "" + costTower , 27, 47);
			}
		}
	}
	
	class TowerStatsPanel extends JPanel {
		private TowerStatsPanel() {
			setLayout( new BorderLayout() );
			setBorder( new EmptyBorder(3, 1, 1, 15) );
			
			towerRepair = new MenuButton("Repair", new ImageIcon("images/buttons/repair.gif"), controller);
			towerSell = new MenuButton("Sell", new ImageIcon("images/buttons/sell.gif"), controller);
			
			towerNameLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			towerPriceLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			towerPowerLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			towerRangeLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			towerFirerateLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			healthLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			towerHealthLabel.setFont( new Font("Verdana",Font.BOLD, 9) );
			
			JPanel topHalf = new JPanel(new GridLayout(1, 2), true);
			
			JPanel statsOfTower = new JPanel(new GridLayout(5, 1), true);
			statsOfTower.setForeground(Color.white);
			statsOfTower.setBackground( backgroundColor );
			
			JPanel healthbarPanel = new JPanel(new GridLayout(2,1));
			healthbarPanel.setBackground( backgroundColor );
			
			JPanel healthButtonPanel = new JPanel(new GridLayout(1,4));
			healthButtonPanel.setBackground(backgroundColor);
			healthButtonPanel.setForeground(Color.white);
			
			JPanel towerImagePanel = new JPanel();
			towerImagePanel.setBackground( backgroundColor );
			
			healthLabel.setForeground( Color.white );
			
			towerNameLabel.setForeground( Color.white );
			towerPriceLabel.setForeground( Color.white );
			towerPowerLabel.setForeground( Color.white );
			towerRangeLabel.setForeground( Color.white );
			towerFirerateLabel.setForeground( Color.white );
			towerHealthLabel.setForeground( Color.white );
			
			statsOfTower.add(towerNameLabel);
			statsOfTower.add(towerPriceLabel);
			statsOfTower.add(towerPowerLabel);
			statsOfTower.add(towerRangeLabel);
			statsOfTower.add(towerFirerateLabel);
			
			towerImagePanel.add(towerImageLabel);
			
			topHalf.add(towerImagePanel);
			topHalf.add(statsOfTower);
			
			healthButtonPanel.add(healthLabel);
			
			JPanel emptyPanel = new JPanel();
			emptyPanel.setBackground(backgroundColor);
			
			healthButtonPanel.add(emptyPanel);
			healthButtonPanel.add(towerRepair);
			healthButtonPanel.add(towerSell);
			
			healthbarPanel.add(healthButtonPanel);
			healthbarPanel.add(towerHealthLabel);

			add(topHalf, BorderLayout.CENTER);
			add(healthbarPanel, BorderLayout.SOUTH);
		}
	}
}
